package com.dengcj.ecology.hrm;


import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;

import java.util.*;

/**
 * @author dengchunjie
 * @Description: 组织架构人力资源相关工具类
 * @date 2022/1/15 12:27
 */
public class HrmUtil {

    /**
     * 获取人员属性
     * @param uid
     * @return
     */
    public static String getUserAttr(int uid,String attr){
        //增加非空判断
        if(StrUtil.isBlank(attr)){
            return "";
        }

        RecordSet rs = new RecordSet();
        rs.executeQuery("select " + attr + " from hrmresource where id=?", uid);
        if(rs.next()){
            return rs.getString(attr);
        }

        return "";
    }

    /**
     * 通过人员属性转换LoginId
     * @param attr 属性名
     * @param attrVal 属性值
     * @return
     */
    public static String getUserLoginIdByAttr(String attr, String attrVal){
        //增加非空判断
        if(StrUtil.isBlank(attr) || StrUtil.isBlank(attrVal)){
            return "";
        }

        RecordSet rs = new RecordSet();
        rs.executeQuery("select loginid from hrmresource where "+attr+"=?", attrVal);
        if(rs.next()){
            return rs.getString("loginid");
        }

        return "";
    }

    /**
     * 通过人员属性转换用户ID
     * @param attr 属性名
     * @param attrVal 属性值
     * @return
     */
    public static int getUserIdByAttr(String attr, String attrVal){
        //增加非空判断
        if(StrUtil.isBlank(attr) || StrUtil.isBlank(attrVal)){
            return -1;
        }

        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from hrmresource where "+attr+"=?", attrVal);
        if(rs.next()){
            return rs.getInt("id");
        }

        return -1;
    }

    /**
     * 获取员工办公地点的名称
     * @param uid
     * @return
     */
    public static String getUserLocationName(String uid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select b.locationname from hrmresource a inner join HrmLocations b on a.locationid=b.id where a.id=?", uid);
        return Util.null2String(rs.getString("locationname"));
    }

    /**
     * 获取部门自定义属性
     * @param deptid
     * @param attr
     * @return
     */
    public static String getDepartmentAttr(String deptid, String attr){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select "+attr+" from HrmDepartmentDefined where deptid=?", deptid);
        if(rs.next()) {
            return Util.null2String(rs.getString(attr));
        }

        return "";
    }


    /**
     * 获取分部属性
     * @param subcompanyid
     * @param attr
     * @return
     */
    public static String getCompanyAttr(String subcompanyid,String attr){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select "+attr+" from HrmSubcompanyDefined where subcomid=?", subcompanyid);
        if(rs.next()) {
            return Util.null2String(rs.getString(attr));
        }

        return "";
    }

    /**
     * 根据人员id获取人员姓名
     * @param uid
     * @return
     */
    public static String getUserLastname(int uid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select lastname from hrmresource where id=?", uid);
        if(rs.next()) {
            return Util.null2String(rs.getString("lastname"));
        }

        return "";
    }

    /**
     * 根据人员id获取部门ID
     * @param uid
     * @return
     */
    public static int getUserDeptId(int uid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select departmentid from hrmresource where id=?", uid);
        if(rs.next()) {
            return Util.getIntValue(Util.null2String(rs.getString("departmentid")));
        }

        return -1;
    }

    /**
     * 根据人员id获取部门名称
     * @param uid
     * @return
     */
    public static String getUserDeptName(int uid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select departmentname from hrmdepartment a inner join hrmresource b on a.id=b.departmentid where b.id=?", uid);
        if(rs.next()) {
            return Util.null2String(rs.getString("departmentname"));
        }

        return "";
    }

    /**
     * 根据人员id获取人员loginid
     * @param uid
     * @return
     */
    public static String getUserLoginId(int uid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select loginid from hrmresource where id=?", uid);
        if(rs.next()){
            return Util.null2String(rs.getString("loginid"));
        }

        return "";
    }

    /**
     * 根据人员id获取人员loginid
     * @param uids
     * @return
     */
    public static List<String> getUserLoginIds(String uids){
        RecordSet rs = new RecordSet();
        List<String> loginIds = new ArrayList<>();
        rs.executeQuery("select loginid from hrmresource where id in(" + uids + ")");
        while(rs.next()){
            loginIds.add(Util.null2String(rs.getString("loginid")));
        }

        return loginIds;
    }

    /**
     * 根据人员loginid获取人员id
     * @param loginid
     * @return
     */
    public static int getUidByLoginid(String loginid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from hrmresource where loginid=?", loginid);
        if(rs.next()) {
            return rs.getInt("id");
        }

        return -1;
    }

    /**
     * 根据人员id获取分部ID
     * @param uid
     * @return
     */
    public static int getUserSubcompanyId(int uid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select subcompanyid1 from hrmresource where id=?", uid);
        if(rs.next()) {
            return Util.getIntValue(Util.null2String(rs.getString("subcompanyid1")));
        }

        return -1;
    }

    /**
     * 根据部门id获取部门名称
     * @param id
     * @return
     */
    public static String getDepartmentName(int id){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select departmentname from hrmdepartment where id=?", id);
        if(rs.next()){
            return Util.null2String(rs.getString("departmentname"));
        }

        return "";
    }

    /**
     * 根据部门id判断该部门是否是末级部门
     * @param id
     * @return
     */
    public static boolean isLeafDept(int id){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select count(*) as rowCount from hrmdepartment where supdepid=?", id);
        return Util.getIntValue(Util.null2String(rs.getString(1))) == 0;
    }

    /**
     * 根据部门id判断该部门是否是顶级部门
     * @param id
     * @return
     */
    public static boolean isTopDept(int id){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select supdepid from hrmdepartment where id=?", id);
        return Util.getIntValue(Util.null2String(rs.getString("supdepid"))) == 0;
    }

    /**
     * 根据分部id获取分部名称
     * @param id
     * @return
     */
    public static String getSubcompanyName(int id){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select subcompanyname from hrmsubcompany where id=?", id);
        if(rs.next()) {
            return Util.null2String(rs.getString("subcompanyname"));
        }

        return "";
    }

    /**
     * 获取部门的一级部门
     * @param deptid 部门id
     * @return 一级部门id
     */
    public static int getSupDept(int deptid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id,supdepid from hrmdepartment where id=?", deptid);
        return Util.getIntValue(Util.null2String(rs.getString("supdepid")));
    }

    /**
     * 获取部门的一级部门信息
     * @param deptid
     * @return  deptMap
     */
    public static Map<String,Object> getSupDeptInfo(int deptid){
        List<Map<String, Object>> deptAllLevel = getDeptAllLevel(deptid);

        if(deptAllLevel.size() > 0){
            return deptAllLevel.get(0);
        }
        return null;
    }

    /**
     * 获取部门的所有层级
     * @param deptid
     * @return
     */
    public static List<Map<String,Object>> getDeptAllLevel(int deptid){
        List<Map<String,Object>> deptList = new ArrayList<>();
        RecordSet rs = new RecordSet();
        if(rs.getDBType().equalsIgnoreCase("sqlserver")){
            rs.executeQuery("WITH cte AS ( " +
                    "SELECT id, supdepid, departmentname, 0 AS lv " +
                    "FROM HrmDepartment c " +
                    "WHERE id = ? " +
                    "UNION ALL " +
                    "SELECT d.Id,d.supdepid,d.departmentname,lv + 1 " +
                    "FROM cte c " +
                    "INNER JOIN HrmDepartment d ON c.supdepid = d.Id) " +
                    "SELECT * FROM cte ORDER BY cte.lv DESC", deptid);

            while(rs.next()){
                Map deptMap = new HashMap<>();
                deptMap.put("id",rs.getInt("id"));
                deptMap.put("name",Util.null2String(rs.getString("departmentname")));

                deptList.add(deptMap);
            }
        } else {
            do {
                rs.executeQuery("select id,departmentname,supdepid from hrmdepartment where id=?", deptid);
                if (rs.next()) {
                    Map deptMap = new HashMap<>();
                    deptMap.put("id", rs.getInt("id"));
                    deptMap.put("name", Util.null2String(rs.getString("departmentname")));

                    deptList.add(deptMap);
                    deptid = Util.getIntValue(Util.null2String(rs.getString("supdepid")), 0);
                }
            } while (deptid != 0);

            return ListUtil.reverse(deptList);
        }

        return deptList;
    }

    /**
     * 根据人员id字符串获取人员姓名字符串
     * @param ids
     * @return
     */
    public static List<String> getUserLastnameList(String ids){
        //增加非空判断
        if("".equals(ids)){
            return null;
        }

        RecordSet rs = new RecordSet();
        List<String> lastnameList = new ArrayList<>();
        rs.executeQuery("select lastname from hrmresource where id in("+ids+")");
        while (rs.next()){
            String lastname = Util.null2String(rs.getString("lastname"));
            if(!"".equals(lastname)) {
                lastnameList.add(lastname);
            }
        }

        return lastnameList;
    }

    /**
     * 判断部门是否是该分部下的部门
     * @param subcompanyid 分部id
     * @param deptid   部门id
     * @return
     */
    public static boolean isSubcompanyDept(String subcompanyid,String deptid){
        //增加非空判断
        if("".equals(subcompanyid) || "".equals(deptid)){
            return false;
        }

        RecordSet rs = new RecordSet();
        rs.executeQuery("select count(id) as rowCount from HrmDepartment where Subcompanyid1=? and id=?", subcompanyid, deptid);
        rs.next();
        return rs.getInt("rowCount") > 0;
    }

    /**
     * 判断人员是否是该分部下的人员
     * @param subcompanyid 分部id
     * @param uid   部门id
     * @return
     */
    public static boolean isSubcompanyUser(String subcompanyid,String uid){
        //增加非空判断
        if("".equals(subcompanyid) || "".equals(uid)){
            return false;
        }

        RecordSet rs = new RecordSet();
        rs.executeQuery("select count(id) as rowCount from hrmresource where subcompanyid1="+subcompanyid+" and id="+uid);
        rs.next();
        return rs.getInt("rowCount") > 0;
    }
}
